package cn.z2huo.knowledge.basic.datetime.newtime;

import java.time.*;
import java.util.Date;

/**
 * <p>
 *
 * @author z2huo
 */
public class EpochTest {

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
        // 与 1970 年 1 月 1 日的差距
        System.out.println(LocalDate.now().toEpochDay());
        System.out.println(LocalDate.now().plusDays(-LocalDate.now().toEpochDay()));
        System.out.println(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        System.out.println(LocalDateTime.now().plusSeconds(-LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"))));
        System.out.println();

        // 当前时刻
        System.out.println(Instant.now());
        // 获取当前时间戳，距离 1970 年 1 月 1 日的毫秒数
        System.out.println(Instant.now().toEpochMilli());
        // 指定时刻
        LocalDateTime customDateTime = LocalDateTime.of(2025, 1, 2, 3, 4, 5);
        System.out.println(customDateTime.toEpochSecond(ZoneOffset.of("+8")));
        // 从距离 1970 年 1 月 1 日 00:00:00.000 的毫秒数的差值获取时刻
        // 没有指定时区的时刻 Instant
        System.out.println(Instant.ofEpochSecond(1735758245L));
        // 指定了时区的时刻
        System.out.println(Instant.ofEpochMilli(1735758245000L).atZone(ZoneId.of("Asia/Shanghai")));
        System.out.println(Instant.ofEpochMilli(1735758245000L).atZone(ZoneId.systemDefault()));
        System.out.println();

        System.out.println(ZoneId.of("Asia/Shanghai"));
        System.out.println(ZoneId.of("CTT", ZoneId.SHORT_IDS));
        System.out.println(ZoneId.systemDefault());
        System.out.println();

        LocalDateTime dateTime = LocalDateTime.of(2024, 4, 9, 18, 54);
        System.out.println(dateTime);
        // LocalDateTime 转 Instant
        System.out.println(dateTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant());
        System.out.println(dateTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant().toEpochMilli());
        System.out.println(Instant.ofEpochMilli(1712660040000L));
        System.out.println(Instant.ofEpochMilli(1712660040000L).atZone(ZoneId.of("Asia/Shanghai")));
        System.out.println();

        // Instant 转 LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1288834974657L), ZoneId.systemDefault());
        System.out.println(localDateTime);
        System.out.println(LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));
        System.out.println();

        System.out.println(new Date(1288834974657L));
        System.out.println();

        System.out.println(LocalDate.EPOCH.toEpochDay());
        LocalDate localDate = LocalDate.ofEpochDay(1288834974657L - LocalDate.EPOCH.toEpochDay());
        System.out.println(localDate);
    }

}
